using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace cs2335.lab5
{
    public class Button : IComponent
    {

        protected Rectangle buttonRectangle = new Rectangle(0, 0, 100, 25);
        protected StringFormat labelFormat = new StringFormat(StringFormatFlags.NoClip);
        protected SolidBrush buttonBrush = new SolidBrush(Color.Ivory);
        protected Font font = new Font("SansSerif", 10);
        protected CCTForm owner;
        protected string text;
        protected bool hasFocus = false;
        protected bool isDown = false;
        protected bool hidden = false;
        protected string name;
        

        #region Properties
        /// <summary>
        /// Tells whether or not a component is hidden. If it is hidden,
        /// it should not be receiving actions.
        /// </summary>
        public bool Hidden
        {
            get { return hidden; }
        }

        /// <summary>
        /// Stores the width and height of the component
        /// </summary>
        public Size Size
        {
            get { return buttonRectangle.Size; }
            set { buttonRectangle.Size = value; Invalidate(); }
        }

        public Point Location
        {
            get { return buttonRectangle.Location; }
            set { buttonRectangle.Location = value; Invalidate(); }
        }

        public Rectangle Bounds
        {
            get { return buttonRectangle; }
            set { buttonRectangle = value; Invalidate(); }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public System.Drawing.Font Font
        {
            get { return font; }
            set { font = value; Invalidate(); }
        }

        public string Text
        {
            get { return text; }
            set { text = value; Invalidate(); }
        }

        public CCTForm Owner
        {
            get { return owner; }
            set { owner = value; }
        }

        public Color BackColor
        {
            get { return buttonBrush.Color; }
            set { buttonBrush.Color = value; Invalidate(); }
        }
        #endregion

        #region Events
        public event EventHandler Click;
        public event EventHandler GotFocus;
        public event EventHandler KeyPress;
        public event EventHandler KeyDown;
        public event EventHandler LostFocus;
        public event EventHandler Paint;
        #endregion


        public Button()
        {
            labelFormat.LineAlignment = StringAlignment.Center;
            labelFormat.Alignment = StringAlignment.Center;
            text="click me"; 
        }

        public void Invalidate()
        {
            if (owner != null)
            {
                owner.Invalidate();
            }
        }

        public void Hide()
        {
            hidden = true;
            Invalidate();
        }

        public void Show()
        {
            hidden = false;
            Invalidate();
        }


        public void OnPaint(PaintEventArgs e)
        {
            if (!hidden)
            {
                Graphics g = e.Graphics;
                if (!isDown)
                {
                    g.FillRectangle(buttonBrush, buttonRectangle);
                    Draw3DBorder(g);
                    g.DrawString(text, font, Brushes.Black, (RectangleF)buttonRectangle, labelFormat);
                }
                if (isDown)
                {
                    DrawDown(g);
                    //g.DrawRectangle(Pens.Blue, buttonRectangle.Left, buttonRectangle.Top, buttonRectangle.Width, buttonRectangle.Height);
                }

                if (Paint != null)
                {
                    Paint(this, e);
                }
            }

        }

        public void OnGotFocus(EventArgs e)
        {
            hasFocus = true;
            if (GotFocus != null)
            {
                GotFocus(this, e);
            }
            owner.Invalidate();
        }

        public void OnLostFocus(EventArgs e)
        {
            hasFocus = false;
            if (LostFocus != null)
            {
                LostFocus(this, e);
            }
            owner.Invalidate();
        }


        public void OnMouseClick(System.Windows.Forms.MouseEventArgs e)
        {
            if (Click != null)
            {
                Click(this, e);
            }
        }

        public void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
        {
            isDown = false;
            Invalidate();
        }

        public void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            isDown = true;
            Invalidate();
        }

        public void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
        {
            /* ignore */
        }

        public void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
        {
            //Invokes a Click event if the user hits ENTER
            //  while this component has focus
            if (e.KeyChar == '\r')
            {
                if (Click != null)
                {
                    Click(this, e);
                }
            }

            if (KeyPress != null)
            {
                 KeyPress(this, e);
            }
        }

        public void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
        {
            if (KeyDown != null)
            {
                KeyDown(this, e);
            }
        }

        private void Draw3DBorder(Graphics g)
        {
            int PenWidth = (int)Pens.White.Width;

            g.DrawLine(Pens.PaleGoldenrod, new Point(buttonRectangle.Left, buttonRectangle.Top),
                new Point(buttonRectangle.Right - PenWidth, buttonRectangle.Top));
            g.DrawLine(Pens.PaleGoldenrod, new Point(buttonRectangle.Left, buttonRectangle.Top),
                new Point(buttonRectangle.Left, buttonRectangle.Bottom));
            g.DrawLine(Pens.DarkGoldenrod, new Point(buttonRectangle.Left, buttonRectangle.Bottom),
                new Point(buttonRectangle.Right - PenWidth, buttonRectangle.Bottom));
            g.DrawLine(Pens.DarkGoldenrod, new Point(buttonRectangle.Right - PenWidth, buttonRectangle.Bottom),
                new Point(buttonRectangle.Right - PenWidth, buttonRectangle.Top));
        }

        private void DrawDown(Graphics g)
        {
            g.FillRectangle(Brushes.DarkKhaki, buttonRectangle);
            int PenWidth = (int)Pens.White.Width;

            g.DrawLine(Pens.DarkGoldenrod, new Point(buttonRectangle.Left, buttonRectangle.Top),
                new Point(buttonRectangle.Right - PenWidth, buttonRectangle.Top));
            g.DrawLine(Pens.DarkGoldenrod, new Point(buttonRectangle.Left, buttonRectangle.Top),
                new Point(buttonRectangle.Left, buttonRectangle.Bottom));
            g.DrawLine(Pens.PaleGoldenrod, new Point(buttonRectangle.Left, buttonRectangle.Bottom),
                new Point(buttonRectangle.Right - PenWidth, buttonRectangle.Bottom));
            g.DrawLine(Pens.PaleGoldenrod, new Point(buttonRectangle.Right - PenWidth, buttonRectangle.Bottom),
                new Point(buttonRectangle.Right - PenWidth, buttonRectangle.Top));

            g.DrawString(text, font, Brushes.Black, (RectangleF)buttonRectangle, labelFormat);
        } 
    }
}
